package us.icitap.cm.business.entities;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToOne;
import javax.persistence.Table;

import org.eclipse.persistence.annotations.Customizer;

import us.icitap.cm.business.entities.regmin.States;
import us.icitap.cm.utils.MyCustomizer;

@Entity
@Customizer(MyCustomizer.class)
@Table(name="DRUG", schema="MPC")
public class Drug implements Serializable{

	/**
	 * 
	 */
	private static final long serialVersionUID = 8599560508576061132L;


	@Id
	@Column(insertable=false, updatable = false)
	private Integer id;
	

	@OneToOne
	@JoinColumn(name="ID",nullable=false)
	private AppEntity appEntity;
	
	
	@ManyToOne
	@JoinColumn(name="DRUG_CLASS_ID")
	private AppList drugClassification;
	
	@ManyToOne
	@JoinColumn(name="DRUG_TYPE_ID")
	private AppList drugType;
	
	@Enumerated(EnumType.STRING)
	private UnitEnum unit;
	
	private double quantity;
	
	@ManyToOne
	@JoinColumn(name="ORIGIN_COUNTRY_ID")
	private States originCountry;
	
	@ManyToOne
	@JoinColumn(name="DEST_COUNTRY_ID")
	private States destinationCountry;
	
	@ManyToOne
	@JoinColumn(name="TRANS_COUNTRY_ID")
	private States transitCountry;

	
	@Column(length=500)
	private String notes;
	
	public Drug() {
		// TODO Auto-generated constructor stub
	}
	
	
	public Drug(Drug drug) {
		this.destinationCountry = drug.getDestinationCountry();
		this.drugClassification = drug.getDrugClassification();
		this.drugType = drug.getDrugType();
		this.notes = drug.getNotes();
		this.originCountry = drug.getOriginCountry();
		this.quantity = drug.getQuantity();
		this.transitCountry = drug.getTransitCountry();
		this.unit = drug.getUnit();
	}

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public AppEntity getAppEntity() {
		return appEntity;
	}

	public void setAppEntity(AppEntity appEntity) {
		this.appEntity = appEntity;
	}

	public AppList getDrugClassification() {
		return drugClassification;
	}

	public void setDrugClassification(AppList drugClassification) {
		this.drugClassification = drugClassification;
	}

	public AppList getDrugType() {
		return drugType;
	}

	public void setDrugType(AppList drugType) {
		this.drugType = drugType;
	}

	public UnitEnum getUnit() {
		return unit;
	}

	public void setUnit(UnitEnum unit) {
		this.unit = unit;
	}

	public double getQuantity() {
		return quantity;
	}

	public void setQuantity(double quantity) {
		this.quantity = quantity;
	}

	public States getOriginCountry() {
		return originCountry;
	}

	public void setOriginCountry(States originCountry) {
		this.originCountry = originCountry;
	}

	public States getDestinationCountry() {
		return destinationCountry;
	}

	public void setDestinationCountry(States destinationCountry) {
		this.destinationCountry = destinationCountry;
	}

	public States getTransitCountry() {
		return transitCountry;
	}

	public void setTransitCountry(States transitCountry) {
		this.transitCountry = transitCountry;
	}

	

	public String getNotes() {
		return notes;
	}

	public void setNotes(String notes) {
		this.notes = notes;
	}

	@Override
	public String toString() {
		
		if(drugClassification != null){
			if(drugType != null) return drugClassification.getTag() + ", " + drugType.getTag();
			else return drugClassification.getTag();
		}
		return "Droga nr." + id;
	}


	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime
				* result
				+ ((destinationCountry == null) ? 0 : destinationCountry
						.hashCode());
		result = prime
				* result
				+ ((drugClassification == null) ? 0 : drugClassification
						.hashCode());
		result = prime * result
				+ ((drugType == null) ? 0 : drugType.hashCode());
		result = prime * result + ((id == null) ? 0 : id.hashCode());
		result = prime * result + ((notes == null) ? 0 : notes.hashCode());
		result = prime * result
				+ ((originCountry == null) ? 0 : originCountry.hashCode());
		long temp;
		temp = Double.doubleToLongBits(quantity);
		result = prime * result + (int) (temp ^ (temp >>> 32));
		result = prime * result
				+ ((transitCountry == null) ? 0 : transitCountry.hashCode());
		result = prime * result + ((unit == null) ? 0 : unit.hashCode());
		return result;
	}


	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Drug other = (Drug) obj;
		if (destinationCountry == null) {
			if (other.destinationCountry != null)
				return false;
		} else if (!destinationCountry.equals(other.destinationCountry))
			return false;
		if (drugClassification == null) {
			if (other.drugClassification != null)
				return false;
		} else if (!drugClassification.equals(other.drugClassification))
			return false;
		if (drugType == null) {
			if (other.drugType != null)
				return false;
		} else if (!drugType.equals(other.drugType))
			return false;
		if (id == null) {
			if (other.id != null)
				return false;
		} else if (!id.equals(other.id))
			return false;
		if (notes == null) {
			if (other.notes != null)
				return false;
		} else if (!notes.equals(other.notes))
			return false;
		if (originCountry == null) {
			if (other.originCountry != null)
				return false;
		} else if (!originCountry.equals(other.originCountry))
			return false;
		if (Double.doubleToLongBits(quantity) != Double
				.doubleToLongBits(other.quantity))
			return false;
		if (transitCountry == null) {
			if (other.transitCountry != null)
				return false;
		} else if (!transitCountry.equals(other.transitCountry))
			return false;
		if (unit != other.unit)
			return false;
		return true;
	}
	
	
	public boolean myEquals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Drug other = (Drug) obj;
		if (destinationCountry == null) {
			if (other.destinationCountry != null)
				return false;
		} else if (!destinationCountry.equals(other.destinationCountry))
			return false;
		if (drugClassification == null) {
			if (other.drugClassification != null)
				return false;
		} else if (!drugClassification.equals(other.drugClassification))
			return false;
		if (drugType == null) {
			if (other.drugType != null)
				return false;
		} else if (!drugType.equals(other.drugType))
			return false;
		
		if (notes == null) {
			if (other.notes != null)
				return false;
		} else if (!notes.equals(other.notes))
			return false;
		if (originCountry == null) {
			if (other.originCountry != null)
				return false;
		} else if (!originCountry.equals(other.originCountry))
			return false;
		if (Double.doubleToLongBits(quantity) != Double
				.doubleToLongBits(other.quantity))
			return false;
		if (transitCountry == null) {
			if (other.transitCountry != null)
				return false;
		} else if (!transitCountry.equals(other.transitCountry))
			return false;
		if (unit != other.unit)
			return false;
		return true;
	}
	
}
